package com.bootmybatisdemo.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bootmybatisdemo.demo.domain.User;
import com.bootmybatisdemo.demo.result.AjaxResult;
import com.bootmybatisdemo.demo.service.IUserService;
import com.bootmybatisdemo.demo.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-05-10
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/userList")
    @ResponseBody
    public AjaxResult userList(){
        IPage<User> userPage = new Page<>(1, 2);//参数一是当前页，参数二是每页个数
//        return AjaxResult.success("成功",userService.page(userPage,new QueryWrapper<>()));
        return AjaxResult.success("成功");
    }

    /**
     * 导入测试
     * @param file 导入文件
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        List<User> userList = util.importExcel(file.getInputStream());
        return AjaxResult.success(userList);
    }

    /**
     * 导出
     * 再调 /download?fileName=123.xlsx&delete=true/false
     */
    @RequestMapping("/export")
    @ResponseBody
    public AjaxResult export(User user)
    {
        List<User> list = userService.list(new QueryWrapper<>());
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "online");
    }
}
